<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-6-3
 * Func   充值
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('user_money_recharge');
$utable = Table('user');

if(IS_POST)
{
	$username = NoBadStr($_POST['username']);
	$money = round($_POST['money'],2);
	
	strlen($username) < 4 && AjaxReturn(1,'请输入正确的账号');
	$money <= 0 && AjaxReturn(1,'充值金额不能为0');
    
    $user = $db->getrow('SELECT id FROM '.$utable.' WHERE username = \''.$username.'\'');
	!$user && AjaxReturn(1,'该账号不存在');
    
	$notes = NoBadStr($_POST['notes']);
	
	//	充值记录	
	$tmp = [];
	$tmp['uid']		=	$user['id'];
	$tmp['payment']	=	'admin';
	$tmp['money']	=	$money;
	$tmp['paytime']	=	TIMESTAMP;
	$tmp['status']	=	1;
	$tmp['ip']		=	$_Y['ip'];
	$tmp['dateline']=	TIMESTAMP;
	$tmp['notes']	=	$notes;
	$id = $db->add('user_money_recharge',$tmp);
	!$id && AjaxReturn(1,'添加充值失败，请重新添加');
    
	//	资金日志
    $tmp = [];
	$tmp['uid']		=	$user['id'];
	$tmp['balance']	=	$money;
	$tmp['tid']		=	'recharge';
	$tmp['lkey']	=	'recharge_admin_'.$_uid.'_'.$user['id'].'_'.$id;
	$tmp['money']	=	$money;
	$tmp['notes']	=	'后台充值：'.$notes;
	$res = MoneyLog($tmp);
	if(!$res)
	{
		$db->exec('UPDATE '.$table.' SET paytime = 0,status = 0 WHERE id = '.$id);
		
		AjaxReturn(1,'添加充值失败，请重新添加');
	}
	rechargeCheckLevel(array('uid' => $user['id'], 'recharge_money' => $money));
	AjaxReturn(0,'添加充值成功！');
}

if($a == 'add')
{
	$data = [];
	
    $tpl->display('user/recharge_add');
}
else if($a == 'ok')
{
    $id = intval($_GET['id']);
	!$id && ShowMsg('充值信息不存在！','?m='.$m.'&c='.$c);
	
	$data = $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id);
	!$data && ShowMsg('充值信息不存在！','?m='.$m.'&c='.$c);
	$data['status'] && ShowMsg('该充值已经到账！','?m='.$m.'&c='.$c);
	
	//	资金日志
	$tmp = [];
	$tmp['uid']		=	$data['uid'];
	$tmp['balance']	=	$data['money'];
	$tmp['tid']		=	'recharge';
	$tmp['lkey']	=	'recharge_admin_'.$_uid.'_'.$data['uid'].'_'.$data['id'];
	$tmp['money']	=	$data['money'];
	$tmp['notes']	=	'充值后台补单';
	$res = MoneyLog($tmp);
	!$res && ShowMsg('操作失败，请重新操作','?m='.$m.'&c='.$c);
	
	$db->exec('UPDATE '.$talbe.' SET paytime = '.TIMESTAMP.',status = 1,notes = \'后台到账\' WHERE id = '.$data['id']);
	
	ShowMsg('操作成功','?m='.$m.'&c='.$c);
}
else
{
	$statuss = ['未支付','已支付'];
	$payments = ['alipay'=>'支付宝','weixin'=>'微信','bank'=>'银行卡','admin'=>'后台充值'];
	
	$data  = array();
	$where = ' FROM '.$table.' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE 1 ';
    
    $uid = intval($_GET['uid']);
	$uid && $where .= ' AND t.uid = '.$uid;
	
	//	时间
	$dt1 = NoBadStr($_GET['dt1']);
	$dt1 && $where .= ' AND t.dateline >= '.strtotime($dt1);
	$dt2 = NoBadStr($_GET['dt2']);
	$dt2 && $where .= ' AND t.dateline <= '.strtotime($dt2);
    
    $payment = NoBadStr($_GET['payment']);
	$payment && $where .= ' AND t.payment = \''.$payment.'\'';
    
    $status = intval($_GET['status']);
	$status && $where .= ' AND t.status = '.($status > 0 ? $status : 0);
    
	$title = NoBadStr($_GET['title']);
	$title && $where .= ' AND (t.notes LIKE \'%'.$title.'%\' OR u.username LIKE \''.$title.'\') ';

	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
	if($tmp['p'])
	{
		$page = max(1,intval($_GET['page']));
		$size = 20;
		$limit    = ' LIMIT '.($page - 1) * $size.','.$size;
		
		$orderby = ' ORDER BY t.id DESC ';
		$url = '?m='.$m.'&c='.$c.'&a='.$a.'&title='.$title.'&status='.$status.'&uid='.$uid.'&payment='.$payment.'&dt1='.$dt1.'&dt2='.$dt2;
		
		$data = $db->getall('SELECT t.*,u.username '.$where.$orderby.$limit);
		
		$multi = Multi($url,$page,$tmp['p'],$size);
	}
	
	$tpl->display('user/recharge');
}